import * as PIXI from "../../../../../../libs/pixi.4.8.0";
import {fgui} from "../../../../../../libs/fairygui.1.3.0";
import MVCApp from "../../../../../mvc/MVCApp";
import GameContext from "../../../../GameContext";
import Commands from "../../constants/Commands";
import ViewNames from "../../constants/ViewNames";
import ModelNames from "../../constants/ModelNames";
import MainModel from "../../model/MainModel";
import RegisterViewController from "../../controller/RegisterViewController";
import RemoveViewController from "../../controller/RemoveViewController";
import MainView from "../MainView";
import {designHeight, designWidth} from "../../../../../app";

export let gContext = new GameContext();

export default class Main extends MVCApp {

    constructor(options) {
        super();
        options.preserveDrawingBuffer = true;
        options.clearBeforeRender = true;
        options.antialias       = true;
        options.width           = options.width             || designWidth;
        options.height          = options.height            || designHeight;
        options.backgroundColor = options.backgroundColor   || 0;
        gContext.app = new PIXI.Application(options);
        gContext.app.stage.interactive = true;

        let w = designWidth;
        let h = designHeight;

        gContext.container = new PIXI.Container();
        gContext.container.interactive = true;
        gContext.container.pivot.set(-w / 2, -h / 2);
        gContext.app.stage.addChild(gContext.container);

        let uiRoot = new fgui.GRoot();
        uiRoot.x = w / 2;
        uiRoot.y = h / 2;
        uiRoot.attachTo(
            gContext.app,
            {
                designWidth:  designWidth,
                designHeight: designHeight,
                resolution:   options.resolution,
                scaleMode:    'showAll',
                orientation:  'auto',
                alignV:       4,
                alignH:       1,
            }
        );

        gContext.uiContainer = new fgui.GComponent();
        uiRoot.addChild(gContext.uiContainer);

        this.startup();
    }

    startup() {
        // -- registerController -----------------------------------------------------
        this.registerController(Commands.REGISTER_VIEW, RegisterViewController);
        this.registerController(Commands.REMOVE_VIEW, RemoveViewController);

        // -- registerModel ----------------------------------------------------------
        this.registerModel(ModelNames.MAIN, MainModel);

        // -- registerView -----------------------------------------------------------
        this.registerView(ViewNames.MAIN, MainView, this);
    }
}